import ParentHook from '../Hook.js';
import FormOperation from './FormOperation.js';

let index = 1;

class Hook extends ParentHook {
	constructor(...args) {
		super(...args);
		this.uiName = 'element';
		this.optionsValueField = 'label';
	}
	hookNameManger({ ctx, nameManger }) {
		// 构造Jiess响应式对象proxy
		const proxy = this.$m.$reactive(nameManger.values);
		// 使用新组装的代理数据置换原对象
		nameManger.values = ctx.frame.initResponsiveData(proxy);
	}
	hookInitValues(nameManger) {
		return { ...nameManger.staticValues };
	}
	hookItemCall(buildItem) {
		const {
			groupConfig,
			defaultValue = null,
			valueName = 'value',
			eventName = 'onInput'
		} = this.initValue(buildItem);
		let { $name, ...o } = buildItem.info;
		// 目前仅针对vue环境初始化基础参数
		if ($name) {
			const baseData = { defaultValue, valueName, eventName };
			// 根据$name参数，解析得到临时的name字符串
			o.name = buildItem.area.nameManger.input($name, baseData);
			// 只有在vue环境中才需要绑定表单事件
			o = this.hookInitParam(buildItem.area, baseData, o);
		}
		// 下拉框，单选框，复选框
		return groupConfig ? this.initGroup(buildItem, groupConfig, o) : o;
	}
	hookFormItem(...args) {
		return this.initFormItem('prop', ...args);
	}
	hookFormParam(area, param) {
		const ref = `Jiess_formRef${index++}`;
		const model = area.nameManger.values;
		area.formOper = new FormOperation(area, ref);
		// 注：一定需要指定labelWidth，否者不能横向对齐
		return { ref, model, labelWidth: '88px', ...param };
	}
	// ----------------------------------------------------------
	hookBuildRule(msg, callback) {
		callback(msg || undefined);
	}
	hookGetOptionsSelect(cb1, text, key) {
		cb1(param => param.label = text);
	}
	hookDialog(show) {
		return {
			visible: true,
			beforeClose: () => show.value = false,
		};
	}
}
export default Hook;